package com.system.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * @author lisiyu
 * @create 2022-10-21 21:10
 * @version1.0
 */
@Component
public class MyPreFilter3 extends ZuulFilter {
    //声明过滤器的类型：前置 后置 错误
    @Override
    public String filterType() {
        //相当于return "pre"
        return FilterConstants.PRE_TYPE;
    }
    //顺序 排序 过滤器的顺序，数字越小，优先级越高
    @Override
    public int filterOrder() {
        return 100;
    }
    //是否启用当前过滤器
    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        //获取到前面的过滤器传递的值
        boolean sendZuulResponse = currentContext.sendZuulResponse();
        return sendZuulResponse;
    }
    //过滤器要执行的代码
    @Override
    public Object run() throws ZuulException {
        System.out.println("run3方法执行了");
        RequestContext currentContext = RequestContext.getCurrentContext();
        Object name = currentContext.get("name");
        System.out.println(name);
        //currentContext.setSendZuulResponse(false);
        return null;
    }
}
